/**
 * 
 */
package mdb.com.vn.web.utils;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;

/**
 * @author tu.dod
 *
 */
public class SSOLogin {
	
	 static LdapContext  ldapContext; 
	 public static void login(String username, String password) {
	   String ldapServer = "ldap://10.76.5.12";//địa chỉ server LDap vd:10.0.0.1 
       String port = "389";//Cổng của server LDap  
       String domain = "MDB";//Tên domain 
      
           Hashtable<String, String> env = new Hashtable<String, String>(11); 
           env.put(Context.INITIAL_CONTEXT_FACTORY,  
                   "com.sun.jndi.ldap.LdapCtxFactory"); 
           env.put(Context.PROVIDER_URL, ldapServer + ":" + port); 
           env.put(Context.SECURITY_AUTHENTICATION, "simple"); 
           env.put(Context.SECURITY_PRINCIPAL, domain + "\\" + username); 
           env.put(Context.SECURITY_CREDENTIALS, password); 
           try { 
        	   ldapContext = new InitialLdapContext(env,null); 
        	   ldapContext.setRequestControls(null);
               NamingEnumeration<?> namingEnum = ldapContext.search("DC=mdb,dc=com,dc=vn", "(samaccountname=truc.ttt)", getSimpleSearchControls());
                   SearchResult result = (SearchResult) namingEnum.next ();    
                   Attributes attrs = result.getAttributes ();
                   System.out.println(attrs.get("physicalDeliveryOfficeName").toString().replace("physicalDeliveryOfficeName:", "").trim());
                   System.out.println(attrs.get("cn"));
                   System.out.println(attrs.get("aCSPolicyName"));
                   System.out.println(attrs.get("adminCount"));
                   System.out.println(attrs.get("adminDescription"));
                   System.out.println(attrs.get("adminDisplayName"));
                   System.out.println(attrs.get("allowedAttributes"));
                   System.out.println(attrs.get("allowedAttributesEffective"));
                   System.out.println(attrs.get("allowedChildClasses"));
                   System.out.println(attrs.get("allowedChildClassesEffective"));
                   System.out.println(attrs.get("altSecurityIdentities"));
                   System.out.println(attrs.get("assistant"));
                   System.out.println(attrs.get("badPasswordTime"));
                   System.out.println(attrs.get("badPwdCount"));
                   System.out.println(attrs.get("bridgeheadServerListBL"));
                   System.out.println(attrs.get("c"));
                   System.out.println(attrs.get("canonicalName"));
                   System.out.println(attrs.get("cn"));
                   System.out.println(attrs.get("co"));
                   System.out.println(attrs.get("codePage"));
                   System.out.println(attrs.get("comment"));
                   System.out.println(attrs.get("company"));
                   System.out.println(attrs.get("controlAccessRights"));
                   System.out.println(attrs.get("countryCode"));
                   System.out.println(attrs.get("createTimeStamp"));
                   System.out.println(attrs.get("dBCSPwd"));
                   System.out.println(attrs.get("defaultClassStore"));
                  System.out.println(attrs.get("department"));
                   System.out.println(attrs.get("description"));
                   System.out.println(attrs.get("desktopProfile"));
                   System.out.println(attrs.get("destinationIndicator"));
                   System.out.println(attrs.get("directReports"));
                   System.out.println(attrs.get("displayName"));
                   System.out.println(attrs.get("displayNamePrintable"));
                   System.out.println(attrs.get("distinguishedName"));
                   System.out.println(attrs.get("division"));
                   System.out.println(attrs.get("dSASignature"));
                   System.out.println(attrs.get("dSCorePropagationData"));
                   System.out.println(attrs.get("dynamicLDAPServer"));
                   System.out.println(attrs.get("employeeID"));
                   System.out.println(attrs.get("extensionName"));
                   System.out.println(attrs.get("facsimileTelephoneNumber"));
                   System.out.println(attrs.get("flags"));
                   System.out.println(attrs.get("fromEntry"));
                   System.out.println(attrs.get("frsComputerReferenceBL"));
                   System.out.println(attrs.get("fRSMemberReferenceBL"));
                   System.out.println(attrs.get("fSMORoleOwner"));
                   System.out.println(attrs.get("garbageCollPeriod"));
                   System.out.println(attrs.get("generationQualifier"));
                   System.out.println(attrs.get("givenName"));
                   System.out.println(attrs.get("groupMembershipSAM"));
                   System.out.println(attrs.get("groupPriority"));
                   System.out.println(attrs.get("groupsToIgnore"));
                   System.out.println(attrs.get("homeDirectory"));
                   System.out.println(attrs.get("homeDrive"));
                   System.out.println(attrs.get("homePhone"));
                   System.out.println(attrs.get("homePostalAddress"));
                   System.out.println(attrs.get("info"));
                   System.out.println(attrs.get("initials"));
                   System.out.println(attrs.get("instanceType"));
                   System.out.println(attrs.get("internationalISDNNumber"));
                   System.out.println(attrs.get("ipPhone"));
                   System.out.println(attrs.get("isCriticalSystemObject"));
                   System.out.println(attrs.get("isDeleted"));
                   System.out.println(attrs.get("isPrivilegeHolder"));
                   System.out.println(attrs.get("l"));
                   System.out.println(attrs.get("lastKnownParent"));
                   System.out.println(attrs.get("lastLogoff"));
                   System.out.println(attrs.get("lastLogon"));
                   System.out.println(attrs.get("legacyExchangeDN"));
                   System.out.println(attrs.get("lmPwdHistory"));
                   System.out.println(attrs.get("localeID"));
                   System.out.println(attrs.get("lockoutTime"));
                   System.out.println(attrs.get("logonCount"));
                   System.out.println(attrs.get("logonHours"));
                   System.out.println(attrs.get("logonWorkstation"));
                   System.out.println(attrs.get("mail"));
                   System.out.println(attrs.get("managedObjects"));
                   System.out.println(attrs.get("manager"));
                   System.out.println(attrs.get("masteredBy"));
                   System.out.println(attrs.get("maxStorage"));
                   System.out.println(attrs.get("memberOf"));
                   System.out.println(attrs.get("mhsORAddress"));
                   System.out.println(attrs.get("middleName"));
                   System.out.println(attrs.get("mobile"));
                   System.out.println(attrs.get("modifyTimeStamp"));
                   System.out.println(attrs.get("mS-DS-ConsistencyChildCount"));
                   System.out.println(attrs.get("mS-DS-ConsistencyGuid"));
                   System.out.println(attrs.get("mS-DS-CreatorSID"));
                   System.out.println(attrs.get("mSMQDigests"));
                   System.out.println(attrs.get("mSMQDigestsMig"));
                   System.out.println(attrs.get("mSMQSignCertificates"));
                   System.out.println(attrs.get("mSMQSignCertificatesMig"));
                   System.out.println(attrs.get("msNPAllowDialin"));
                   System.out.println(attrs.get("msNPCallingStationID"));
                   System.out.println(attrs.get("msNPSavedCallingStationID"));
                   System.out.println(attrs.get("msRADIUSCallbackNumber"));
                   System.out.println(attrs.get("msRADIUSFramedIPAddress"));
                   System.out.println(attrs.get("msRADIUSFramedRoute"));
                   System.out.println(attrs.get("msRADIUSServiceType"));
                   System.out.println(attrs.get("msRASSavedCallbackNumber"));
                   System.out.println(attrs.get("msRASSavedFramedIPAddress"));
                   System.out.println(attrs.get("msRASSavedFramedRoute"));
                   System.out.println(attrs.get("name"));
                   System.out.println(attrs.get("netbootSCPBL"));
                   System.out.println(attrs.get("networkAddress"));
                   System.out.println(attrs.get("nonSecurityMemberBL"));
                   System.out.println(attrs.get("ntPwdHistory"));
                   System.out.println(attrs.get("nTSecurityDescriptor"));
                   System.out.println(attrs.get("o"));
                   System.out.println(attrs.get("objectCategory"));
                   System.out.println(attrs.get("objectClass"));
                   System.out.println(attrs.get("objectGUID"));
                   System.out.println(attrs.get("objectSid"));
                   System.out.println(attrs.get("objectVersion"));
                   System.out.println(attrs.get("operatorCount"));
                   System.out.println(attrs.get("otherFacsimileTelephoneNumber"));
                   System.out.println(attrs.get("otherHomePhone"));
                   System.out.println(attrs.get("otherIpPhone"));
                   System.out.println(attrs.get("otherLoginWorkstations"));
                   System.out.println(attrs.get("otherMailbox"));
                   System.out.println(attrs.get("otherMobile"));
                   System.out.println(attrs.get("otherPager"));
                   System.out.println(attrs.get("otherTelephone"));
                   System.out.println(attrs.get("otherWellKnownObjects"));
                   System.out.println(attrs.get("ou"));
                   System.out.println(attrs.get("pager"));
                   System.out.println(attrs.get("partialAttributeDeletionList"));
                   System.out.println(attrs.get("partialAttributeSet"));
                   System.out.println(attrs.get("personalTitle"));
                   System.out.println(attrs.get("physicalDeliveryOfficeName"));
                   System.out.println(attrs.get("possibleInferiors"));
                   System.out.println(attrs.get("postalAddress"));
                   System.out.println(attrs.get("postalCode"));
                   System.out.println(attrs.get("postOfficeBox"));
                   System.out.println(attrs.get("preferredDeliveryMethod"));
                   System.out.println(attrs.get("preferredOU"));
                   System.out.println(attrs.get("primaryGroupID"));
                   System.out.println(attrs.get("primaryInternationalISDNNumber"));
                   System.out.println(attrs.get("primaryTelexNumber"));
                   System.out.println(attrs.get("profilePath"));
                   System.out.println(attrs.get("proxiedObjectName"));
                   System.out.println(attrs.get("proxyAddresses"));
                   System.out.println(attrs.get("pwdLastSet"));
                   System.out.println(attrs.get("queryPolicyBL"));
                   System.out.println(attrs.get("registeredAddress"));
                   System.out.println(attrs.get("replPropertyMetaData"));
                   System.out.println(attrs.get("replUpToDateVector"));
                   System.out.println(attrs.get("repsFrom"));
                   System.out.println(attrs.get("repsTo"));
                   System.out.println(attrs.get("revision"));
                   System.out.println(attrs.get("rid"));
                   System.out.println(attrs.get("sAMAccountName"));
                   System.out.println(attrs.get("sAMAccountType"));
                   System.out.println(attrs.get("scriptPath"));
                   System.out.println(attrs.get("sDRightsEffective"));
                   System.out.println(attrs.get("securityIdentifier"));
                   System.out.println(attrs.get("seeAlso"));
                   System.out.println(attrs.get("serverReferenceBL"));
                   System.out.println(attrs.get("servicePrincipalName"));
                   System.out.println(attrs.get("showInAddressBook"));
                   System.out.println(attrs.get("showInAdvancedViewOnly"));
                   System.out.println(attrs.get("sIDHistory"));
                   System.out.println(attrs.get("siteObjectBL"));
                   System.out.println(attrs.get("sn"));
                   System.out.println(attrs.get("st"));
                   System.out.println(attrs.get("street"));
                   System.out.println(attrs.get("streetAddress"));
                   System.out.println(attrs.get("subRefs"));
                   System.out.println(attrs.get("subSchemaSubEntry"));
                   System.out.println(attrs.get("supplementalCredentials"));
                   System.out.println(attrs.get("systemFlags"));
                   System.out.println(attrs.get("telephoneNumber"));
                   System.out.println(attrs.get("teletexTerminalIdentifier"));
                   System.out.println(attrs.get("telexNumber"));
                   System.out.println(attrs.get("terminalServer"));
                   System.out.println(attrs.get("textEncodedORAddress"));
                   System.out.println(attrs.get("thumbnailLogo"));
                   System.out.println(attrs.get("thumbnailPhoto"));
                  System.out.println(attrs.get("title"));
                   System.out.println(attrs.get("tokenGroups"));
                   System.out.println(attrs.get("tokenGroupsGlobalAndUniversal"));
                   System.out.println(attrs.get("tokenGroupsNoGCAcceptable"));
                   System.out.println(attrs.get("unicodePwd"));
                   System.out.println(attrs.get("url"));
                   System.out.println(attrs.get("userAccountControl"));
                   System.out.println(attrs.get("userCert"));
                   System.out.println(attrs.get("userCertificate"));
                   System.out.println(attrs.get("userParameters"));
                   System.out.println(attrs.get("userPassword"));
                   System.out.println(attrs.get("userPrincipalName"));
                   System.out.println(attrs.get("userSharedFolder"));
                   System.out.println(attrs.get("userSharedFolderOther"));
                   System.out.println(attrs.get("userSMIMECertificate"));
                   System.out.println(attrs.get("userWorkstations"));
                   System.out.println(attrs.get("uSNChanged"));
                   System.out.println(attrs.get("uSNCreated"));
                   System.out.println(attrs.get("uSNDSALastObjRemoved"));
                   System.out.println(attrs.get("USNIntersite"));
                   System.out.println(attrs.get("uSNLastObjRem"));
                   System.out.println(attrs.get("uSNSource"));
                   System.out.println(attrs.get("wbemPath"));
                   System.out.println(attrs.get("wellKnownObjects"));
                   System.out.println(attrs.get("whenChanged"));
                   System.out.println(attrs.get("whenCreated"));
                   System.out.println(attrs.get("wWWHomePage"));
                   System.out.println(attrs.get("x121Address"));

//                   System.out.println(attrs.get("cn"));
//                   System.out.println(attrs.get("name"));
//                   System.out.println(attrs.get("homemta"));
//                   System.out.println(attrs.get("company"));
//                   System.out.println(attrs.get("uid"));
//                   System.out.println(attrs.get("ou"));
                   //System.out.println(attrs.toString());
//                   System.out.println(attrs.get("E-Mail Address"));
//                   System.out.println(attrs.get("Logon Name"));
//                   System.out.println(attrs.get("Logon Workstations"));

                 // } 
               namingEnum.close();
           } catch (Exception e) {
        	   String t =e.getMessage();
        	   System.out.println(t);
        	   String r ="";
        	   r=t.substring(t.indexOf("error code")+10,t.indexOf("error code")+14).trim();
        	 //  System.out.println(lmsg.getLdapMsg(r));
        	   System.out.println(t.indexOf("data",1));
               System.out.println(r);
           }}
	 private static SearchControls getSimpleSearchControls() {
		    SearchControls searchControls = new SearchControls();
		    searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
		    searchControls.setTimeLimit(3);
		    //String[] attrIDs = {"objectGUID"};
		    //searchControls.setReturningAttributes(attrIDs);
		    return searchControls;
		}

}
